﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using AlexTranslator;

namespace TextTools
{

    //{"sentences":
    //[
        //{"trans":"It works.",
        //"orig":"Это работает.",
        //"translit":"",
        //"src_translit":"Eto rabotaet."}],
    //"src":"ru",
    //"server_time":8}

    [Serializable]
    public class AlexTranslation
    {        

        public Inside [] sentences;
        public string src;
        public string server_time;        
        
    }

    [Serializable]
    public class Inside
    {
        public string trans;
        public string orig;
        public string translit;
        public string src_translit;
    };
    //
    //{"responseData": null, 
    //"responseDetails":     "invalid key", 
    //"responseStatus": 400}

    public class GoogleHack
    {
        public static string GetTranslationByPost(string text, string langFrom, string LangTo)
        {
            PostSubmitter post = new PostSubmitter();
            post.Url = "http://translate.google.ru/translate_a/t";
            post.PostItems.Add("client", "x"); 
            post.PostItems.Add("text", HttpUtility.HtmlEncode( text));
            post.PostItems.Add("sl", langFrom);
            post.PostItems.Add("tl", LangTo);
            post.Type = PostSubmitter.PostTypeEnum.Post;
            string json = post.Post();

            string result = GetTranslationFromJson(json);
            return result;
        }

        public static string GetTranslationByGet(string src)
        {
            

            
            string encodedSrc = HttpUtility.UrlEncode(src);
            string _requestUrl = string.Format("http://translate.google.ru/translate_a/t?client=x&text={0}&sl={1}&tl={2}", encodedSrc, "ru", "uk");
            
            
                
                HttpWebRequest request = (HttpWebRequest) WebRequest.Create(_requestUrl);                
                request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7"; // the point!

                WebResponse response = request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream()                    
                );
                string json = reader.ReadToEnd();
                return GetTranslationFromJson(json);
        }

        private static string GetTranslationFromJson(string json)
        {
            string result = "";
            try
            {

                using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
                {
                    DataContractJsonSerializer ser =
                       new DataContractJsonSerializer(typeof(AlexTranslation));
                    AlexTranslation translation = ser.ReadObject(ms) as AlexTranslation;



                    result = translation.sentences[0].trans;

                    result = string.Join("", translation.sentences.Select(p => p.trans));

                }
            }
            catch (Exception)
            {
                throw;
            }

            return result;
        }
    }
}
